package main;

import server.Webserver;

import gui.ChatWindow;
import gui.ChatWindowV2;
import handlers.Contacts;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.ArrayList;

@SuppressWarnings("serial")
public class Main extends JFrame {
    JPanel clist = new JPanel();
    JScrollPane clistcontainer = new JScrollPane();
    DefaultListModel<String> model = new DefaultListModel<String>();
    JList<String> contactList = new JList<String>(model);
    static Integer listen_port = null;

    public Main() {
        try {
            run();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            listen_port = new Integer(args[0]);
        } catch (Exception e) {
            listen_port = new Integer(8080);
        }
        Main webserver = new Main();
    }

    private void run() throws Exception {
//        chatwindow.setBackground(new Color(16, 12, 66));
//        chatwindow.setForeground(new Color(151, 138, 255));
//        chatwindow.setBorder(BorderFactory.createLoweredBevelBorder());
//        chatwindow.setToolTipText("");
//        chatwindow.setEditable(false);
//        chatwindow.setColumns(30);
//        chatwindow.setRows(15);
        clistcontainer.setPreferredSize(new Dimension(130, 450));

        this.setTitle("FancyChat");
        this.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                this_windowClosed(e);
            }
        });
        clistcontainer.getViewport().add(contactList);
        clist.add(clistcontainer);
        
        this.setLayout(new GridLayout(1,1));
        this.getContentPane().add(clist);
        
        this.setVisible(true);
        this.setSize(130, 450);
        this.setResizable(false);
        this.validate();
        
        addContactsToList();
        
        contactList.addMouseListener(new MouseAdapter(){
        	public void mouseClicked(MouseEvent evt){
        		@SuppressWarnings("unchecked")
				JList<String> list = (JList<String>)evt.getSource();
        		if(evt.getClickCount() == 2){
        			int i = list.locationToIndex(evt.getPoint());
        			String name = list.getModel().getElementAt(i); 
        			String a = "";
					try {
						a = Contacts.getContactList().get(name);
					} catch (IOException e) {}
        			System.out.println();
//        			new ChatWindowV2(name, a.split(":")[0], Integer.parseInt(a.split(":")[1]));
        			new ChatWindowV2(name);
        		}
        	}
        	
        });
        new Webserver(listen_port.intValue(), this);
    }

    void this_windowClosed(WindowEvent e) {
        System.exit(1);
    }

//    public void send_message_to_window(String s) {
//        chatwindow.append(s);
//    }
    
    public void addContactsToList() throws IOException{
    	for(String name :Contacts.getContactList().keySet()){
    		model.addElement(name);
    	}
    }
    
    public void addContact(String name){
    	model.addElement(name);
    }
}